/*
* $Id: ChangeFamilyOperation.cpp 95 2010-05-27 20:18:24Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ChangeFamilyOperation.h"
#include "FieldValuesOperation.h"
#include "../Tables/FamiliesTable.h"
#include "Event/EventId.h"

CChangeFamilyOperation::CChangeFamilyOperation(){}
CChangeFamilyOperation::~CChangeFamilyOperation(){}

CChangeFamilyOperation::content::content(){
	Clear();
}
void CChangeFamilyOperation::content::Clear(){
	s_nIdPoint = 0;
	s_nType = 0;
	s_nId = 0;
	s_strName.clear();
	s_strNote.clear();
	s_aUserFields.clear();
}

void CChangeFamilyOperation::UpdateValue(IDatabase* pDatabase){
	pDatabase->BeginTransaction(_T("Update Family"));
	CFamiliesTable families;
	families.m_content.s_nId = m_content.s_nId;
	IResult* pResult = families.GetValue(pDatabase);
	int nIdRecord = 0, nIdQueenBee = 0, nIdPoint = 0;
	if(pResult->GetSizeResult()){
		nIdRecord = ((CFamiliesTable::content*)(*pResult)[0])->s_nIdRecord;
		nIdQueenBee = 0;
		nIdPoint = ((CFamiliesTable::content*)(*pResult)[0])->s_nIdPoint;
	}
	pResult->Free();
	CFieldValuesOperation fieldValues;
	fieldValues.m_content.s_nId = nIdRecord;
	fieldValues.m_content.s_table = CFieldValuesOperation::content::FAMILIES;
	fieldValues.m_content.s_aUserFields = m_content.s_aUserFields;
	fieldValues.UpdateValue(pDatabase);
	nIdRecord = fieldValues.m_content.s_nId;
	families.m_content.s_nId = m_content.s_nId;
	families.m_content.s_nIdPoint = nIdPoint;
	families.m_content.s_strName = m_content.s_strName;
	families.m_content.s_type = (CFamiliesTable::content::type)m_content.s_nType;
	families.m_content.s_strNote = m_content.s_strNote;
	families.m_content.s_nIdRecord = nIdRecord;
	families.UpdateValue(pDatabase);
	pDatabase->EndTransaction(_T("Update Family"));
	IApplication::GetApp()->GetEventManager()->SendEvent(FAMILIES_DB_CHANGE_EVENT);
}